package com.chen.base.utils;


import com.chen.base.emun.ResultCode;
import lombok.Data;
import org.springframework.lang.NonNull;


/**
 * Response返回对像
 *
 * @author Owen
 * @version 1.0
 * @date 2025/3/15
 */
@Data
public class R<T> {

    private String status;

    private String message;

    private T data;

    private R() {
        status = ResultCode.SUCCESS.getCode();
        message = ResultCode.SUCCESS.getMessage();
    }

    public static <T> R<T> error() {
        return error(ResultCode.ERROR);
    }

    public static <T> R<T> error(String message) {
        R<T> r = new R<>();
        r.status = ResultCode.ERROR.getCode();
        r.message = message;
        return r;
    }

    public static <T> R<T> error(ResultCode code) {
        R<T> r = new R<>();
        r.status = code.getCode();
        r.message = code.getMessage();
        return r;
    }

    public static <T> R<T> error(String code, String message) {
        R<T> r = new R<>();
        r.status = code;
        r.message = message;
        return r;
    }

    public static <T> R<T> ok() {
        return new R<>();
    }

    public static <T> R<T> ok(String message) {
        R<T> r = new R<>();
        r.message = message;
        return r;
    }

    public static <T> R<T> ok(ResultCode resultCode) {
        R<T> r = new R<>();
        r.message = resultCode.getMessage();
        r.status = resultCode.getCode();
        return r;
    }

    public static <T> R<T> ok(String status, String message) {
        R<T> r = new R<>();
        r.message = message;
        r.status = status;
        return r;
    }

    public static <T> R<T> okOrError(boolean isTure, String successMessage, String errorMessage) {
        if (isTure) {
            return ok(successMessage);
        } else {
            return error(errorMessage);
        }

    }

    public static <T> R<T> logicError(String message) {
        R<T> r = new R<>();
        r.status = ResultCode.LOGIC_ERROR.getCode();
        r.message = message;
        return r;
    }

    /**
     * 新数据返回对像
     *
     * @param data 数据返回对像
     * @return R<T>
     */
    public R<T> data(@NonNull T data) {
        this.data = data;
        return this;
    }
}
